#include <iostream>
#include "vector"
#include "algorithm"

using namespace std;

/*
 * 常用查找算法
 *      find(iterator begin, iterator end, value)            查找元素，找到返回指定元素的迭代器，找不到返回结束迭代器
 *      find_if(iterator begin, iterator end, _Pred)         按条件查找元素，找到返回指定元素的迭代器，找不到返回结束迭代器
 *                                                  _Pred 函数或谓词（返回bool类型的仿函数）
 *      adjacent_find(iterator begin, iterator end)   查找相邻重复元素,返回相邻重复元素的第一个位置的迭代器
 *      binary_search(iterator begin, iterator end, value)   二分查找法,查到返回true，否则返回false  注 容器必须有序
 *      count(iterator begin, iterator end, value)           统计元素个数
 *      count_if(iterator begin, iterator end, _Pred)        按条件统计元素个数  _Pred 函数或谓词（返回bool类型的仿函数）
 */

class Person{
public:
    string p_name;
    int p_age;
    Person(string name, int age){
        this->p_name = name;
        this->p_age = age;
    }
    bool operator==(const Person & p){
        if(this->p_name == p.p_name && this->p_age == p.p_age){
            return true;
        }
        return false;
    }
};

void test01(){
    cout << "find查找内置数据类型" << endl;
    vector<int> v;
    for (int i = 0; i < 10; ++i) {
        v.push_back(i);
    }
    vector<int>::iterator it = find(v.begin(), v.end(), 5);
    if(it == v.end()){
        cout << "未找到" << endl;
    } else{
        cout << "找到了" << endl;
    }
    cout << "find查找自定义数据类型" << endl;
    vector<Person> v2;
    Person p1("a", 1);
    Person p2("b", 2);
    Person p3("c", 3);
    Person p4("d", 4);
    v2.push_back(p1);
    v2.push_back(p2);
    v2.push_back(p3);
    v2.push_back(p4);
    Person pp("c", 3);
    vector<Person>::iterator it2 = find(v2.begin(), v2.end(), pp);
    if(it == v.end()){
        cout << "未找到" << endl;
    } else{
        cout << "找到了" << endl;
    }
}

class MyEqual{
public:
    bool operator()(int value){
        return value == 5;
    }
};

void test02(){
    cout << "find_if查找内置数据类型" << endl;
    vector<int> v;
    for (int i = 0; i < 10; ++i) {
        v.push_back(i);
    }
    vector<int>::iterator it = find_if(v.begin(), v.end(), MyEqual());
    if(it == v.end()){
        cout << "未找到" << endl;
    } else{
        cout << "找到了" << endl;
    }
}

int main() {

    test01();
    test02();

    return 0;
}
